package com.sc.ifs.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.common.pagination.Page;
import com.sc.dal.common.pagination.PaginationBean;
import com.sc.dal.dto.FavortiesDto;
import com.sc.dal.mapper.favorites.FavoritesMapper;
import com.sc.dal.model.favorites.Favorites;
import com.sc.service.BaseServiceImpl;
import com.sc.service.favorites.FavoritesService;
import com.sc.util.adapter.ConfigBeanAdapter;
import com.sc.util.data.Constants;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;
@Controller
@RequestMapping("/app/favorites")
public class AppMemberFavoritesController extends BaseServiceImpl<Favorites>{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private FavoritesService favoritesService;
	
	@RequestMapping("/selectByPage")
	@ResponseBody
	public Map<String, Object> selectPagination(String mId,String collectionType, int currPage,int pageSize){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Page page = new Page();
		//传入页号
		page.setCurrPage(currPage);
		//传入每页记录数
		page.setPageSize(pageSize);
		Favorites favorites=new Favorites();
		favorites.setmId(mId);
		favorites.setCollectionType(collectionType);
		List<Favorites> resultList = new ArrayList<Favorites>();
		List<FavortiesDto> data = new ArrayList<FavortiesDto>();
		int totalRecords = 0;
		int totalPage = 0;
		try {
			PaginationBean<Favorites> result = favoritesService.getPagingDatas(favorites, page, "count", "selectByParam",FavoritesMapper.class);
			resultList = result.getPageList();
			for (Object object : resultList) {
				FavortiesDto favortiesDto=(FavortiesDto)object;
				String img=favortiesDto.getImg();
				if(img!=null&&!"".equals(img)){
					String[] imgArr=img.split(";");
					if(imgArr.length>0){
						favortiesDto.setImg(ConfigBeanAdapter.fileAccessAddress+imgArr[0]);
					}
				}
				data.add(favortiesDto);
			}
			totalRecords = result.getTotalRecords();
			totalPage = result.getTotalPage();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS, totalRecords, totalPage, resultList);;
		} catch (Exception e) {
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("分页查询流水账表异常！");
			throw new ScBizException("分页查询流水账表异常！", e);
		}	
		return resultMap;
	}
	@RequestMapping("/deleteFavorites")
	@ResponseBody
	public Map<String, Object> deleteFavorites(String fids){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		try {
			String[] fidsArray=null;
			if(fids!=null){
				fidsArray=fids.split(",");
			}
			for (int i = 0; i < fidsArray.length; i++) {
				Favorites favorites=new Favorites();
				favorites.setId(fidsArray[i]);
				favorites.setIsDelete(Constants.IS_DELETEB);
				favoritesService.updateByPrimaryKeySelective(favorites);
			}
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
		} catch (Exception e) {
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_DELETE);
		}
		
		return resultMap;
	}
}
